List
A value of class List is an ordered collection of values. The values contained in a list are known as items. Each item can belong to any class.LITERAL EXPRESSIONS
A list appears in a script as a series of expressions contained within braces and separated by commas. For example,
{ "it's", 2, true }is a list containing a string, an integer, and a Boolean.Each list item can be any valid expression. For example,
{ "it" & "'s", 1 + 1, 4 > 3 }has the same value as the list in the previous example, because each of the expressions it contains has the same value as the corresponding expression
in the previous example.An empty list is a list containing no items. It is represented by a pair of
empty braces:
{}PROPERTIES
Class
- The class identifier for the value. This property is read-only, and its value is always
list
.Length
- An integer containing the number of items in the list. This property is read-only.
Rest
- A list containing all items in the list except the first item.
Reverse
- A list containing all items in the list, but in the opposite order.
ELEMENT
Item
- A value contained in the list. Each value contained in a list
is an item. You can refer to values by their item numbers.
For example,item 2 of { "soup", 2, "nuts" }
is the integer2
. To specify items of a list, use the reference forms listed in "Reference Forms" later in this definition.OPERATORS
The operators that can have List values as operands are &, =, ≠, Starts With, Ends With, Contains, Is Contained By.For detailed explanations and examples of how AppleScript operators treat lists, see "Operators That Handle Operands of Various Classes," which begins on page 168.
COMMANDS HANDLED
You can count the items in a list with the Count command. For example, the value of the following statement is6
.
count {"a", "b", "c", 1, 2, 3} --result: 6You can also count elements of a specific class in a list. For example, the value of the following statement is3
.
count integers in {"a", "b", "c", 1, 2, 3} --result: 3Another way to count the items in a list is with a Length property reference:
length of {"a", "b", "c", 1, 2, 3} --result: 6REFERENCE FORMS
Use the following reference forms to refer to properties of lists and items in lists:
You cannot use the Relative, Name, ID, or Filter reference forms. For example, the following reference, which uses the Filter reference form on a list, is
- Property. For example,
class of {"this", "is", "a", "list"}
specifieslist
.- Index. For example,
item 3 of {"this", "is", "a", "list"}
specifies"a"
.- Middle. For example,
middle item of {"this", "is", "a", "list"}
specifies"is"
.- Arbitrary. For example, some item of {"soup", 2, "nuts"} might specify any of the items in the list.
- Every Element. For example, every item of {"soup", 2, "nuts"} specifies {"soup", 2, "nuts"}.
- Range. For example, items 2 thru 3 of {"soup", 2, "nuts"} specifies {2, "nuts"}.
not valid.
the items in {"this", "is", "a", "list"} whose first ÿ character is "t"--result: not a valid referenceCOERCIONS SUPPORTED
AppleScript supports coercion of a single-item list to any value class to which the item can be coerced if it is not part of a list.AppleScript also supports coercion of an entire list to a string if all items in the list can be coerced to a string. The resulting string concatenates all the items:
{5, "George", 11.43, "Bill"} as string --result: "5George11.43Bill"Individual items in a list can be of any value class, and AppleScript supports coercion of any value to a list that contains a single item. Concatenated values of any class can also be coerced to a list:
5 & "George" & 11.43 & "Bill" as list --result: {5, "George", 11.43, "Bill"}NOTES
To merge or add values to lists, use the concatenation operator (&). For example,
{"This"} & {"is", "a", "list"}results in
{"This", "is", "a", "list"}Note that the concatenation operator merges the items of the two lists into a single list rather than making one list a value within the other list.